//
//  NewChatViewController.swift
//  gamlify
//
//  Created by Admin on 2024/12/9.
//

import UIKit
import SnapKit

class NewChatViewController: UIViewController {

    // MARK: - Properties

    private lazy var greetingLabel: UILabel = {
        let label = UILabel()
        label.text = "Hi Jojolin, How about today?"
        label.font = .systemFont(ofSize: 24, weight: .init(600))
        label.textColor = UIColor.hex(0x4B586B)
        label.numberOfLines = 0
        return label
    }()

    private lazy var dateLabel: UILabel = {
        let label = UILabel()
        label.text = "2024.11.28 16:01"
        label.font = .systemFont(ofSize: 12, weight: .init(400))
        label.textColor = UIColor.hex(0x4B586B)
        return label
    }()

    private lazy var optionsStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.spacing = 10
        return stackView
    }()

    private lazy var inputField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "Say something..."
        textField.backgroundColor = .clear
        return textField
    }()

    private lazy var sendButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "send_button"), for: .normal)
        button.setImage(UIImage(named: "send_button"), for: .highlighted)
        return button
    }()
    
    // sendViewWapper
    private lazy var sendViewWapper: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.hex(0xEDEDFF)
        view.layer.borderWidth = 1
        view.layer.borderColor = UIColor.hex(0xCECEFF).cgColor
        view.layer.cornerRadius = 6
        return view
    }()

    // MARK: - Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        view.endEditing(true)
    }

    // MARK: - UI Setup

    private func setupUI() {
        view.backgroundColor = .clear

        view.addSubview(greetingLabel)
        view.addSubview(dateLabel)
        view.addSubview(optionsStackView)
        view.addSubview(sendViewWapper)
        view.addSubview(inputField)
        view.addSubview(sendButton)

        setupConstraints()
        setupOptionsStackView()
    }

    private func setupConstraints() {
        greetingLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(26)
            make.leading.equalToSuperview().offset(26)
            make.trailing.equalToSuperview().inset(20)
        }

        dateLabel.snp.makeConstraints { make in
            make.top.equalTo(greetingLabel.snp.bottom).offset(10)
            make.leading.equalTo(greetingLabel)
        }

        optionsStackView.snp.makeConstraints { make in
            make.bottom.equalTo(inputField.snp.top).offset(-101)
            make.leading.trailing.equalToSuperview().inset(20)
            make.height.equalTo(100)
        }

        sendViewWapper.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(26)
            make.trailing.equalToSuperview().inset(21)
            make.bottom.equalToSuperview().offset(-(AppDimensions.shared.bottomToTabBarTop + 25))
            make.height.equalTo(44)
        }
        
        inputField.snp.makeConstraints { make in
            make.leading.equalTo(sendViewWapper).inset(16)
            make.top.bottom.equalTo(sendViewWapper)
            make.right.equalTo(sendButton.snp.left).inset(8)
        }

        sendButton.snp.makeConstraints { make in
            make.width.equalTo(64)
            make.height.equalTo(32)
            make.centerY.equalTo(sendViewWapper)
            make.trailing.equalTo(sendViewWapper).inset(6)
        }
    }

    private func setupOptionsStackView() {
        let options = ["rainbow", "car", "book"]
        for imageName in options {
            let imageView = UIImageView()
            imageView.image = UIImage(named: imageName)
            optionsStackView.addArrangedSubview(imageView)
        }
    }
}
